package com.czh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czh.domain.entity.Photo;
import com.czh.domain.entity.Photospan;
import com.czh.domain.vo.PhotoShowVo;
import com.czh.mapper.PhotoMapper;
import com.czh.service.PhotoService;
import com.czh.service.PhotospanService;
import com.czh.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Photo)表服务实现类
 *
 * @author makejava
 * @since 2024-05-07 20:05:23
 */
@Service("photoService")
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {
    @Resource
    private PhotospanService photospanService;

    @Override
    public List<PhotoShowVo> getPhotoList() {
        List<PhotoShowVo> photoShowVos = BeanCopyUtils.copyBeanList(list(), PhotoShowVo.class);
        LambdaQueryWrapper<Photospan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //TODO 时间复杂度太高,目前数据量少,有时间在改进,看着有点帅
        return photoShowVos.stream().peek(photoShowVo -> {
            lambdaQueryWrapper.eq(Photospan::getPhotoId, photoShowVo.getId());
            ArrayList<String> spans = photospanService.getBaseMapper().selectList(lambdaQueryWrapper).stream().map(Photospan::getSpan).collect(Collectors.toCollection(ArrayList::new));
            lambdaQueryWrapper.clear();
            photoShowVo.setSpan(spans);
        }).collect(Collectors.toList());
    }

    @Override
    public List<String> getImgList() {
        ArrayList<String> urls = new ArrayList<>();
        list().forEach(photo -> urls.add(photo.getUrl()));
        return urls;
    }
}

